<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AssetsLoader
 *
 * @author JeyDotC
 */
class AssetsLoader {

    private static function isDebuging() {
        return ModulesContext::instance()->getConfig()->get("debug");
    }

    private static function getMinFileName($assetFile, $type) {
        $min = ".min";
        $ext = ".$type";
        $minAssetFile = str_replace($ext, $min . $ext, $assetFile);
        return $minAssetFile;
    }

    private static function assetExists($assetFile, $type) {
        $path = Loader::toSinglePath("assets::$type::$assetFile", "");
        return is_file($path);
    }

    private static function getRealFile($assetFile, $type) {
        $resultingFile = $assetFile;

        if (!self::isDebuging()) {
            $resultingFile = self::getMinFileName($assetFile, $type);
            if (!self::assetExists($resultingFile, $type)) {
                $resultingFile = $assetFile;
            }
        }

        if (!self::assetExists($resultingFile, $type)) {
            $basePath = Loader::toSinglePath("assets::$type", "");
            throw new Exception("The asset '$resultingFile' of type '$type' doesn't exist in $basePath");
        }

        return $resultingFile;
    }

    public static function script($assetFile) {
        $src = self::getRealFile($assetFile, "js");
        JHTML::_("script", $src, "components/com_acaweb/assets/js/");
    }

    public static function css($assetFile) {
        $file = self::getRealFile($assetFile, "css");
        JHTML::_("stylesheet", $file, "components/com_acaweb/assets/css/");
    }

    public static function urlScript($url) {
        $document = & JFactory::getDocument();
        $document->addScript($url);
    }

    public static function sciptTag($scriptContent) {
        $document = &JFactory::getDocument();
        $document->addScriptDeclaration($scriptContent);
    }

}

?>
